﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using NHibernate.Hql.Ast;
using NHibernate.Linq;
using NHibernate.Linq.Functions;
using NHibernate.Linq.Visitors;

namespace NHDay_Linq.ProviderExtensions
{
	/// <summary>
	/// The actual generator that contains the methods that build the expression to be used by the linq provider
	/// 
	/// it 'translate' our IsLike() function call to the actual SQL LIKE operator
	/// </summary>
	public class IsLikeGenerator : BaseHqlGeneratorForMethod
	{
		public IsLikeGenerator()
		{
			SupportedMethods = new[] { ReflectionHelper.GetMethodDefinition(() => IsLikeLinqExtension.IsLike(null, null)) };
		}

		public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject,
			ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
		{
			return treeBuilder.Like(visitor.Visit(arguments[0]).AsExpression(),
									visitor.Visit(arguments[1]).AsExpression());
		}
	}
}
